using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using Material.Entity;
using Material.Object;
using System.Configuration;

namespace Material.App
{
    public partial class FrmBarang : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        private TblBarang tblBarang;
        private TblBarangEntity tblBarangEntity;
        private string stateInput = string.Empty;
        private int IdBarang = 0;


        public FrmBarang()
        {
           
            InitializeComponent();
            ClearTextbox();
            EnabledTextboxs(false, false,false,false,false,false,false,false,false);
            EnabledButtons(true, false, false, true);
            BindGridData();
            BindCBSatuan();
            BindCBKelompok();

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
           
            cbListParameter.SelectedIndex = 0;
        }

        /// <summary>
        /// Kondisi untuk Disable dan Enable Kontrol [Textbox dan Button]
        /// </summary>
        /// <param name="state">boolean True dan False</param>
        private void EnabledTextboxs(bool bltxtNamaBarang, bool bltxtKeterangan,
            bool blcbKelompok,bool blcbSatuan,bool bltxtJumlah,
            bool bltxtharga1,bool bltxtprosen1,bool bltxtprosen2,
            bool bltanggal)
        {
            txtNamaBarang.Enabled = bltxtNamaBarang;
            cbKelompok.Enabled = blcbKelompok;
            cbSatuan.Enabled = blcbSatuan;
            txtJumlah.Enabled = bltxtJumlah;
            txtHarga1.Enabled = bltxtharga1;
            txtDisc1.Enabled = bltxtprosen1;
            txtDisc2.Enabled = bltxtprosen2;
            tglBeli.Enabled = bltanggal;

        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnCancel, bool blbtnSave, bool blbtnPrint)
        {
            btnNew.Enabled = blbtnNew;
            btnCancel.Enabled = blbtnCancel;
            btnSave.Enabled = blbtnSave;
            btnPrint.Enabled = blbtnPrint;

        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            //txtKeterangan.Text = string.Empty;
            txtNamaBarang.Text = string.Empty;
            txtJumlah.Text = "0";
            txtHarga1.Text = "0";
            txtHarga2.Text = "0";
            txtHarga3.Text = "0";
            txtDisc1.Text = "0";
            txtDisc2.Text = "0";
            txtTotal.Text = "0";
        }

        private void FrmSatuan_Load(object sender, EventArgs e)
        {

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtNamaBarang.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Nama Barang Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNamaBarang.Focus();
                return;
            }
            else
            {
                tblBarang = new TblBarang();
                tblBarangEntity = new TblBarangEntity(connStr);
                int i = 0;

                tblBarang.NamaBarang = txtNamaBarang.Text;
                //tblBarang.Keterangan = txtKeterangan.Text;
                tblBarang.IdKelompok = Convert.ToInt32(cbKelompok.SelectedValue);
                tblBarang.IdSatuan = Convert.ToInt32(cbSatuan.SelectedValue);
                tblBarang.Jumlah = Convert.ToInt32(txtJumlah.Text);
                tblBarang.Harga1 = Convert.ToDecimal(txtHarga1.Text);
                tblBarang.Harga2 = Convert.ToDecimal(txtHarga2.Text);
                tblBarang.Harga3 = Convert.ToDecimal(txtHarga3.Text);
                tblBarang.TotalHarga = Convert.ToDecimal(txtJumlah.Text) * Convert.ToDecimal(txtHarga1.Text);
                tblBarang.TglBeli = DateTime.Now;

                if (stateInput == "Add")
                {
                    tblBarang.CreatedDate = DateTime.Now;
                    tblBarang.CreatedBy = FrmLogin.UserID;
                    tblBarang.ModifiedDate = DateTime.Now;
                    tblBarang.ModifiedBy = FrmLogin.UserID;
                    i = tblBarangEntity.Insert(tblBarang);
                }
                else
                {
                    tblBarang.Id = IdBarang;
                    tblBarang.ModifiedBy = FrmLogin.UserID;
                    tblBarang.ModifiedDate = DateTime.Now;
                    i = tblBarangEntity.Update(tblBarang);
                }
                if (i != -1)
                {
                    ClearTextbox();
                    EnabledTextboxs(false,false, false, false, false,false,false,false,false);
                    EnabledButtons(true, false, false, true);
                    BindGridData();
                    MessageBox.Show("Simpan data berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                    MessageBox.Show("Simpan data tidak berhasil", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);


            }

        }

        private void BindGridData()
        {
            tblBarangEntity = new TblBarangEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblBarangEntity.GetDataAllKelompokSatuan().Tables[0];


        }

        private void BindGridData(string parameter, string value)
        {
            tblBarangEntity = new TblBarangEntity(connStr);
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblBarangEntity.GetFindDataAll(parameter, value).Tables[0];
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(true, true,true,true,true,true,true,true,true);
            EnabledButtons(false, true, true, false);
            txtNamaBarang.Focus();
            stateInput = "Add";
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnabledTextboxs(false, false,false,false,false,false,false,false,false);
            EnabledButtons(true, false, false, true);
            ClearTextbox();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 10:
                        ClearTextbox();
                        EnabledTextboxs(true, true,true,true,true,true,true,true,true);
                        EnabledButtons(false, true, true, false);
                        tblBarangEntity = new TblBarangEntity(connStr);
                        tblBarang = new TblBarang();
                        tblBarang.Id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                        tblBarang = tblBarangEntity.GetDataByID(tblBarang);
                        txtNamaBarang.Text = tblBarang.NamaBarang;
                        cbKelompok.SelectedValue = tblBarang.IdKelompok;
                        cbSatuan.SelectedValue = tblBarang.IdSatuan;
                        if(tblBarang.TglBeli!=null)
                            tglBeli.Value = Convert.ToDateTime(tblBarang.TglBeli);
                        txtJumlah.Text = tblBarang.Jumlah.ToString();
                        txtHarga1.Text = tblBarang.Harga1.ToString();
                        txtHarga2.Text = tblBarang.Harga2.ToString();
                        txtHarga3.Text = tblBarang.Harga3.ToString();
                        txtTotal.Text = tblBarang.TotalHarga.ToString();
                        IdBarang = tblBarang.Id;
                        stateInput = "Edit";
                        break;

                    case 11:
                        DialogResult dr = MessageBox.Show("Data akan dihapus?", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr == DialogResult.Yes)
                        {
                            tblBarangEntity = new TblBarangEntity(connStr);
                            tblBarang = new TblBarang();
                            tblBarang.Id = Convert.ToInt16(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                            int i = tblBarangEntity.Delete(tblBarang);
                            BindGridData();
                        }
                        break;
                }

            }

         
        }
        private void BindCBSatuan()
        {
            TblSatuanEntity tblSatuanEntity = new TblSatuanEntity(connStr);
            cbSatuan.DataSource = tblSatuanEntity.GetDataAll();
            cbSatuan.ValueMember = "Id";
            cbSatuan.DisplayMember = "Satuan";
        }

        private void BindCBKelompok()
        {
            TblKelompokEntity tblKelompokEntity = new TblKelompokEntity(connStr);
            cbKelompok.DataSource = tblKelompokEntity.GetDataAll();
            cbKelompok.ValueMember = "Id";
            cbKelompok.DisplayMember = "Kelompok";
        }

        private void btnPrint_Click(object sender, EventArgs e)
        {
            //FrmRptBarang frm = new FrmRptBarang();
            //frm.ShowDialog();

            FrmDipsPembBarang frm = new FrmDipsPembBarang();
            frm.ShowDialog();
        }

        private void txtDisc1_TextChanged(object sender, EventArgs e)
        {
            txtHarga2.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc1.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));

        }

        private void txtDisc2_TextChanged(object sender, EventArgs e)
        {
            txtHarga3.Text = Convert.ToString((Convert.ToDecimal(txtHarga1.Text) * Convert.ToInt32(txtDisc2.Text)) / 100 + Convert.ToDecimal(txtHarga1.Text));

        }

        private void txtHarga1_TextChanged(object sender, EventArgs e)
        {
            txtDisc1.Text = "0";
            txtDisc2.Text = "0";
            txtHarga2.Text = "0";
            txtHarga3.Text = "0";
        }

        private void cbListParameter_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbListParameter.SelectedIndex == 0)
            {
                txtFind.Visible = false;
                btnFind.Visible = false;
                cbListFind.Visible = false;

                BindGridData();
            }
            else if (cbListParameter.SelectedIndex == 1)
            {
                //cbListFind.Visible = true;
                txtFind.Visible = true;
                btnFind.Visible = true;
                txtFind.Focus();
            }
            else if (cbListParameter.SelectedIndex == 2)
            {
                cbListFind.Visible = true;
                txtFind.Visible = false;
                btnFind.Visible = true;


                TblKelompokEntity tblKelompokEntity = new TblKelompokEntity(connStr);
                cbListFind.DataSource = tblKelompokEntity.GetDataAll();
                cbListFind.ValueMember = "Id";
                cbListFind.DisplayMember = "Kelompok";
            }
            else if (cbListParameter.SelectedIndex == 3)
            {
                cbListFind.Visible = true;
                txtFind.Visible = false;
                btnFind.Visible = true;

                TblSatuanEntity tblSatuanEntity = new TblSatuanEntity(connStr);
                cbListFind.DataSource = tblSatuanEntity.GetDataAll();
                cbListFind.ValueMember = "Id";
                cbListFind.DisplayMember = "Satuan";

            }
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            if (cbListParameter.SelectedIndex == 1)
            {
                if (txtFind.Text.Trim().Length == 0)
                {
                    MessageBox.Show("Parameter pencarian harus diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                else
                {
                    BindGridData("NamaBarang", txtFind.Text.Trim());
                }
            }

            if (cbListParameter.SelectedIndex == 2)
                BindGridData("KelompokBarang", cbListFind.Text);

            if (cbListParameter.SelectedIndex == 3)
                BindGridData("SatuanBarang", cbListFind.Text);
        }

        
        private void txtJumlah_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtHarga1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtDisc1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void txtDisc2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }

        private void dataGridView1_DoubleClick(object sender, EventArgs e)
        {

        }

    }
}